package cn.stylefeng.guns.modular.tool.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.ExcelWrite;
import cn.stylefeng.guns.modular.tool.entity.ToolExpressReconciliationRf;
import cn.stylefeng.guns.modular.tool.mapper.ToolExpressReconciliationRfMapper;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationRfParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressReconciliationYzParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressReconciliationRfResult;
import cn.stylefeng.guns.modular.tool.model.result.ToolExpressReconciliationYzResult;
import  cn.stylefeng.guns.modular.tool.service.ToolExpressReconciliationRfService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 软风对账管理 服务实现类
 * </p>
 *
 * @author zhucx
 * @since 2022-12-27
 */
@Service
public class ToolExpressReconciliationRfServiceImpl extends ServiceImpl<ToolExpressReconciliationRfMapper, ToolExpressReconciliationRf> implements ToolExpressReconciliationRfService {

    @Override
    public void add(ToolExpressReconciliationRfParam param){
        ToolExpressReconciliationRf entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ToolExpressReconciliationRfParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ToolExpressReconciliationRfParam param){
        ToolExpressReconciliationRf oldEntity = getOldEntity(param);
        ToolExpressReconciliationRf newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ToolExpressReconciliationRfResult findBySpec(ToolExpressReconciliationRfParam param){
        return null;
    }

    @Override
    public List<ToolExpressReconciliationRfResult> findListBySpec(ToolExpressReconciliationRfParam param){
        return this.baseMapper.findListBySpec(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ToolExpressReconciliationRfParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ToolExpressReconciliationRfParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ToolExpressReconciliationRf getOldEntity(ToolExpressReconciliationRfParam param) {
        return this.getById(getKey(param));
    }

    private ToolExpressReconciliationRf getEntity(ToolExpressReconciliationRfParam param) {
        ToolExpressReconciliationRf entity = new ToolExpressReconciliationRf();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

	@Override
	public void deleteBySpec(ToolExpressReconciliationRfParam param) {
		this.baseMapper.deleteBySpec(param);
	}

	@Override
	public boolean saveBatchList(List<ToolExpressReconciliationRfParam> list) {
		return this.baseMapper.saveBatchList(list);
	}

	@Override
	public Workbook exportToExcel(Long reconciliationId,String v1) {
	    //这是表头
	    String[] arr = {"客户","出库单号","店铺编号","店铺名称","平台站点","出仓单号","内部订单号","出库日期","状态","快递公司","快递单号","省份","备注","省","市","区","街道","发货件数","打包发货费"
	    		,"计费重量","重量区间","取整重量","快递费","面单费","续重","二三级中转标准","二三级中转最低","中转费","地区加收","疫情加收"
	    		,"特殊城市加收","旺季加收"
	    		,"超区","总快递费"};
	    //这是具体数据
	    List<List<String>> list = new ArrayList<>();
	    
	    ToolExpressReconciliationRfParam param = new ToolExpressReconciliationRfParam();
	    param.setV1(v1);
	    param.setReconciliationId(reconciliationId);
	    List<ToolExpressReconciliationRfResult> datalist = this.findListBySpec(param);
	    for (ToolExpressReconciliationRfResult r : datalist) {
		    List<String> list1 = new ArrayList<String>();
		    list1.add(r.getCustomerName());
		    list1.add(r.getOutNumber());
		    list1.add(r.getShopNumber());
		    list1.add(r.getShopName());
		    list1.add(r.getPlatform());
		    list1.add(r.getOutWNumber());
		    list1.add(r.getInOrderNumber());
		    list1.add(r.getOutTime());
		    list1.add(r.getStates());
		    list1.add(r.getExpressName());
		    list1.add(r.getOrderNumber());
		    list1.add(r.getProvince());
		    list1.add(r.getRemarks());
		    list1.add(r.getProvinces());
		    list1.add(r.getCity());
		    list1.add(r.getArea());
		    list1.add(r.getStreet());
		    list1.add(r.getNums());
		    list1.add(r.getV2());
		    
		    list1.add(r.getChargingWeightKg());
		    list1.add(r.getWeightQj());
		    list1.add(r.getRoundingWeightKg());
		    list1.add(r.getExpressMoney());
		    list1.add(r.getUnilateralism());
		    list1.add(r.getCWeight());
		    list1.add(r.getStandard());
		    list1.add(r.getMinimum());
		    list1.add(r.getTransferMoney());
		    list1.add(r.getAdditional());
		    list1.add(r.getAdditionalE());
		    list1.add(r.getAdditionalSpecial());
		    list1.add(r.getAdditionalBusySeason());
		    list1.add(r.getGoBeyond());
		    list1.add(r.getSumExpressMoney());
		    list.add(list1);
		}
	    return ExcelWrite.writeToExcelByList(arr, list);
	}

}
